<?php

class PackageService
{
    public function getListForPublic()
    {
        $ret = B2bPackage::model()->findAllByAttributes(
                array("type"=>B2bPackage::PUBLIC_USER),
                array("select"=>"id,name,description,org_price,sale_price,small_img",
                    "order"=>"id desc"));
        if($ret == null){
            return JSONHelper::ERROR_SERVICE;
        }
        $items = array();
        foreach ($ret as $it) {
            $item["id"]=$it->id;
            $item["name"]=$it->name;
            $item["org_price"]=$it->org_price/100;
            $item["sale_price"]=$it->sale_price/100;
            $item["description"]=$it->description;
            $item["img"]=Yii::app()->baseUrl."/".$it->small_img;
            $item["centerCount"] = $this->getRelatedCenterCount($it->id);
            $item["soldCount"] = 1;
            array_push($items, $item);
        }
        return $items;
    }
    /**
     * 
     * @param type $id 套餐ID
     * @return 详情
     */
    public function getDetailById($pid)
    {
        $ret = B2bPackage::model()->findByPk($pid);
        if(null === $ret){
            return JSONHelper::ERROR_PACKAGE;
        }
        $item["id"] = $ret->id;
        $item["name"] = $ret->name;
        $item["org_price"] = $ret->org_price/100;
        $item["sale_price"] = $ret->sale_price/100;
        $item["img"] = $ret->big_img;
        $item["examitems"] = $ret->examitems;
        $item["introduce"] = $ret->introduce;
        $item["centers"] = $this->getRelatedCentersById($pid);
        return $item;
        
    }
    /**
     * 套餐关联的体检中心计数
     */
    private function getRelatedCenterCount($pid)
    {
        $sql = "SELECT count(id) as centerCount FROM b2b_package_to_center where `package_id`={$pid}";
     
        $dependency = new CDbCacheDependency("select max(id) from b2b_package_to_center");  
        $ret = B2bPackageToCenter::model()->cache(3600, $dependency)->find(
                    array("select"=>"count(id) as centerCount",
                        "condition"=>"`package_id`={$pid}")
                );
        if($ret != null){
            return $ret->centerCount;
        }
        return 0;
    }
    /**
     * 获取套餐关联的体检中心
     */
    private function getRelatedCentersById($pid)
    {
        $ret = B2bPackageToCenter::model()->findAllByAttributes(array(
                "package_id"=>$pid
            ));
        
        $items = array();
        foreach($ret as $it){
            $it = $it->center;
            if($it === null){
                continue;
            }
            $item["id"] = $it->id;
            $item["name"] = $it->name;
            $item["brandid"] = $it->brand->id;
            $item["brandname"] = $it->brand->name;
            $item["icon"] = Yii::app()->getBaseUrl()."/".$it->brand->icon;
            $item["gps_lat"] = $it->gps_lat;
            $item["gps_lon"] = $it->gps_lon;
            $item["telephone"] = $it->telephone;
            $item["province"] = $it->province;
            $item["city"] = $it->city;
            $item["address"] = $it->address;
            array_push($items, $item);
        }
        return $items;
    }
}
